package luo;
import java.sql.*;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JOptionPane;
import jy.MainUI;
import jy.OrderUI;
import jy.SignUI;
/*
 *登录验证类的控制器
 */
public class Main {
	//登录首页
	private DataBase db;
	public Main(){
		 this.db=new DataBase();
	}
	
	/*
	 * 界面的控制
	 */
	//加载登陆界面
	public static void main(String[] args){		
		MainUI main=new MainUI();
		main.setVisible(true);
	}
	//加载注册界面
	public void go_to_register(){
		SignUI register=new SignUI();
		register.setVisible(true);		
	}
	//加载登录成功后的界面
	public void go_to_login(String room){
		OrderUI login_succ=new OrderUI(room);
		login_succ.setVisible(true);
		
	}
	/*
	 * 信息处理
	 */
	//判断是否为超级管理员
	public void adminstrator(String account,String password){
		if(account=="homework"&&password=="1234"){
			//进入管理员
		}else{
			//停留在原来的界面
			System.out.println("登录失败，请联系管理员！");
		}
	}
	//用户验证
	public void user_validation(String account,String password){
		if(db.user_validation(account, password)){
			//进入订水界面
			try {
				ResultSet result=db.query("select room from user_infor where email="+"'"+account+"' and password="+"'"+password+"'");
				if(result.next()){
					String room=(String)result.getObject(1);
					this.go_to_login(room);							
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}else{
			JOptionPane.showMessageDialog(null, "登录信息错误", "用户登录", 0);
		}
	}
	
	/*
	 * 注册新用户的相关信息
	 * register——information: 
	 * 	1.account
	 *	2.password
	 */
	
	public void register(String email,String room,String password){
		//DataBase db=new DataBase();
		//验证相关信息		
		try {
			if(this.db.update("insert into user_infor values("+"'"+email+"',"+"'"+room+"',"+"'"+password+"')"))
			{			
				System.out.println("注册成功！");			
			}			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//定水
	public boolean book_water(String numb,String room){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String time=(df.format(new Date()));// new Date()为获取当前系统时间
		String price="6";
		try {
			if(this.db.update("insert into water values("
					+"'"+room+"',"+"'"+numb+"',"+"'"+time+"',"+"'"+price+"')" 
					)){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	
	}
	
	//检查水是已经送出去̬
	public void check_water(){
	}
	
	//验证邮箱格式
	 public boolean emailFormat(String email)  {      
		 boolean tag = true;      
		 final String pattern1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";      
		 final Pattern pattern = Pattern.compile(pattern1);      
		 final Matcher mat = pattern.matcher(email);      
		 if (!mat.find()) {          
			 tag = false;      
		 }      
		 return tag;  
	}
	 //验证是否已经注册
	 public boolean is_register(String email){
		 try {
			 ResultSet result=this.db.query("select email from user_infor where email="+"'"+email+"'");
			 if(result.next()){
				 return true;
			 }else{
				 return false;
			 }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			 return false;
		}
	 }
	 
	
}
